package com.blkj.common.core.bean;

import com.blkj.common.exception.base.BaseException;
import lombok.Data;

/**
 * @author 北浪 2021/11/19
 */
@Data
public class R {

    private String code;
    private String msg;
    private Boolean flag;
    private Object data;

    public static R ok(){
        R r = new R();
        r.setCode("200");
        r.setMsg("success");
        r.setFlag(true);
        return r;
    }

    public static R ok(String msg){
        R r = new R();
        r.setMsg(msg);
        return r;
    }

    public static R error(){
        R r = new R();
        r.setCode("500");
        r.setMsg("error");
        r.setFlag(false);
        return r;
    }

    public static R error(String msg){
        R error = R.error();
        error.setMsg(msg);
        return error;
    }

    public R data(Object data){
        this.setData(data);
        return this;
    }

    //自定义异常返回的结果
    public static R defineError(BaseException e){
        R r = new R();
        r.setFlag(false);
        r.setCode(e.getCode());
        r.setMsg(e.getDefaultMessage());
        r.setData(null);
        return r;
    }
}
